package org.study.quartz.quartzmanager.bean;

import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.study.quartz.quartzmanager.QuartzIntegrationJobDetail;
import org.study.quartz.quartzmanager.task.TaskMinuteJob;
import org.study.quartz.quartzmanager.task.TaskScheduler;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/***
 * 定时任务相关管理类信息
 */
@Configuration
@ComponentScan(value = "org.study.quartz")
public class QuartzManagerBeanConfig {

    @Autowired
    private DataSource dataSource;


    @Bean("taskScheduler")
    public TaskScheduler getTaskScheduler() {
        return new TaskScheduler();
    }

    @Bean("taskMinuteJob")
    public TaskMinuteJob getTaskMinuteJob() {
        return new TaskMinuteJob();
    }


    @Bean("taskSchedulerJobDetail")
    public JobDetailFactoryBean getJobDetailFactoryBean() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(QuartzIntegrationJobDetail.class);
        jobDetailFactoryBean.setDurability(true);
        Map<String, Object> map = new HashMap<>();
        map.put("targetBeanName", "taskScheduler");
        map.put("targetMethod", "execute");
        jobDetailFactoryBean.setJobDataAsMap(map);
        return jobDetailFactoryBean;
    }

    @Bean("taskSchedulerJobTigger")
    public CronTriggerFactoryBean getConTriggerFactoryBean() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(getJobDetailFactoryBean().getObject());
        cronTriggerFactoryBean.setCronExpression("0 */1 * * * ?");
        return cronTriggerFactoryBean;
    }


    @Bean("scheduler")
    @Scope(value = "singleton")
    public SchedulerFactoryBean getSchedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setDataSource(this.dataSource);
        schedulerFactoryBean.setConfigLocation(new ClassPathResource("quartz.properties"));
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        schedulerFactoryBean.setAutoStartup(true);
        schedulerFactoryBean.setSchedulerName("defaultScheduler");
        Trigger[] triggers = new Trigger[]{getConTriggerFactoryBean().getObject()};
        schedulerFactoryBean.setTriggers(triggers);
        return schedulerFactoryBean;
    }


}
